import request from '../utils/request'

// 获取用户列表
export function getUserList(params) {
  return request({
    url: '/user/query',
    method: 'get',
    params: {
      keyword: params.username || '',
      pageNum: params.current || 0,
      pageSize: params.size || 10
    }
  })
}

// 新增用户
export function addUser(data) {
  return request({
    url: '/user/add',
    method: 'put',
    params: {
      loginName: data.loginName,
      userName: data.username,
      password: data.password,
      mobile: data.mobile
    }
  })
}

// 更新用户
export function updateUser(data) {
  return request({
    url: '/user/update',
    method: 'post',
    params: {
      id: data.id,
      loginName: data.loginName,
      userName: data.username,
      mobile: data.mobile
    }
  })
}

// 修改手机号
export function updatePhone(data) {
  return request({
    url: '/user/update',
    method: 'post',
    params: data
  })
}

// 修改邮箱
export function updateEmail(data) {
  return request({
    url: '/user/update',
    method: 'post',
    params: data
  })
}

// 修改密码
export function updatePassword(data) {
  return request({
    url: '/user/update',
    method: 'post',
    params: data
  })
}

// 删除用户
export function deleteUser(id) {
  return request({
    url: `/user/delete/${id}`,
    method: 'post'
  })
}

// 登录用户
// export是暴露接口的 
export function login(data) {
  return request({
    url: '/user/login',
    method: 'post',
    // 后端使用@RequestParam接收参数，应该放在params中而不是data中
    params: {
      loginName: data.loginName || data.username,
      password: data.password
    }
  })
}

// 注册用户
export function register(data) {
  return request({
    url: '/user/register',
    method: 'post',
    // 注册也使用@RequestParam，放在params中
    params: {
      loginName: data.username,
      userName: data.username,
      password: data.password,
      mobile: data.phone,
      email: data.email,
      verifyCode: data.captcha
    }
  })
}

// 验证原密码
export function checkPassword(data) {
  return request({
    url: '/user/checkPassword',
    method: 'post',
    params: data
  })
}

// 获取图形验证码
export function getImageCaptcha(key) {
  return `${request.defaults.baseURL}/captcha/image?key=${encodeURIComponent(key)}`
}